<?php
/**
 * MiniBlog database (MySQL) class
 *
 * @version 0.1 
 * @license MIT License http://www.opensource.org/licenses/mit-license.php
 * @copyright (c) 2007 Geir Arne Brevik
 * @author Geir Arne Brevik
 */
if (!defined('MB')) { header('Location:./'); } // prevent direct access to this file

class DB
{
	/**
	 * construct an instance of the DB class by connecting to the server
	 *
	 * @param string $host Address to MySQL database server
	 * @param string $user MySQL username
	 * @param string $password MySQL password
	 * @param string $databse MySQL database
	 *
	 * @author Geir Arne Brevik
	 **/
	private function __construct ($host, $user, $password, $databse)
	{
		// set variables
		$this->host 	= $host;
		$this->user 	= $user;
		$this->password = $password;
		$this->database = $databse;
		
		// connect to database
		$this->link = mysqli_connect($this->host, $this->user, $this->password) or die("Could not find the database. Check configuration in config.php");
		// 
		mysqli_select_db($this->link, $this->database) or die("Could not select database");
	}
	
	/**
	 * return an instance of the DB class, and ensure that we never create more than one
	 * implements the singleton design pattern
	 *
	 * @param string $host Address to MySQL database server
	 * @param string $user MySQL username
	 * @param string $password MySQL password
	 * @param string $databse MySQL database
	 * @return object of the DB class
	 * 
	 * @author Geir Arne Brevik
	 **/
    public function &getInstance ($host, $user, $password, $databse)
    {
        static $instance;
        
        if (!isset($instance)) {
            $instance = new DB($host, $user, $password, $databse);
        }
        
        return $instance;
        
    }
	
	/**
	 * query the database and return resultset
	 *
	 * @param  string $query valid mysql query
	 * @return $result mysqli resultset
	 * @author Geir Arne Brevik
	 **/	
	function doQuery($query) {
		$result = mysqli_query($this->link, $query);
		if (!$result) {
			die('<h1>Database query failed</h1><h2>Query:</h2><pre>'.$query.'</pre><h2>Error:</h2><pre>' . $this->link->error . '</pre>');
		};
		return($result);
	}
	
	/**
	 * get first value in mysql result array
	 *
	 * @param  string $query valid mysql query
	 * @return $result mysqli resultset
	 * @author Geir Arne Brevik
	 **/
	function getFirstInArray($query) {
		$result_array = $this->getArray($query);
		$first = $result_array[0];
		return($first);
	}
	
	/**
	 * Queries the database, and returns the result as an array
	 *
	 * @param  string $query valid mysql query
	 * @return $result mysqli resultset
	 * @author Geir Arne Brevik
	 **/
	function getArray($query) {
		// do the query
		$result = $this->doQuery($query);
	 	// transform into array
	   	$result_rows = $result ? mysqli_fetch_array($result) : false;
		
		return $result_rows;
	}
	
	/**
	 * Check if a given table exists
	 *
	 * @param string $table name of the table
	 * @return bool
	 * @author Geir Arne Brevik
	 **/
	function tableExists($table)
	{
		global $CONFIG;
		
		// look for the table
		$query = mysqli_query($this->link, "SHOW TABLES LIKE '" .$CONFIG['db_prefix']. $table . "'");
		if (mysqli_fetch_row($query) == false) {
			return(false);
		} else {
			return(true);
		}
	 }
	}


?>